<?PHP

$password = $_POST['password'];
$repeat_password = $_POST['repeat_password'];
$name = $_POST['name'];
$sex = $_POST['sex'];
$school = $_POST['school'];
$job_title = $_POST['job_title'];
$team_position = $_POST['team_position'];
$school_position = $_POST['school_position'];
$phone_number = $_POST['phone_number'];
$email = $_POST['email'];
$clothing_model = $_POST['clothing_model'];
$committee = $_POST['committee'];
if (!empty($_POST['bank_account'])) {
    $bank_account = $_POST['bank_account'];
} else {
    $bank_account = '';
}
if (!empty($_POST['bank'])) {
    $bank = $_POST['bank'];
} else {
    $bank = '';
}
if (!empty($_GET['flag'])) {
    $flag = $_GET['flag'];
} else {
    $flag = '';
}
$mysqli = mysqli_init();

$mysqli->options(MYSQLI_OPT_CONNECT_TIMEOUT, 2); //设置超时时间
$mysqli->real_connect('127.0.0.1', 'root', 'mysql');
$sql = "CREATE DATABASE IF NOT EXISTS CUPT DEFAULT CHARSET utf8 COLLATE utf8_general_ci;";
if ($mysqli->query($sql) == TRUE) {
    
}
mysqli_select_db($mysqli, "CUPT");
if ($flag == 0) {
    $sql = "select * from participant where `email`='$email';";
    $rst = $mysqli->query($sql);
    $sql1 = "select * from participant where school='$school' and committee='是';";
    $rst1 = $mysqli->query($sql1);
    if ($repeat_password !== $password) {
        echo "<br />两次输入密码不一致<br />";
        echo '<a href="javascript :;" onClick="javascript :history.back(-1);">回到注册界面</a>';
    } elseif ($rst->num_rows != 0) {
        echo "<br />该邮箱已被注册<br />";
        echo '<a href="javascript :;" onClick="javascript :history.back(-1);">回到注册界面</a>';
    } elseif ($rst1->num_rows != 0 && $committee == '是') {
        echo "<br />一个学校只能有一位竞赛委员会委员<br />";
        echo '<a href="javascript :;" onClick="javascript :history.back(-1);">回到注册界面</a>';
    } else {
//$sql = "describe participant;";
//插入数据
        $sql = "INSERT INTO participant(password,name,sex,school,job_title,team_position,school_position,phone_number,email,bank_account,bank,clothing_model,committee) VALUES('$password','$name','$sex','$school','$job_title','$team_position','$school_position','$phone_number','$email','$bank_account','$bank','$clothing_model','$committee');";
//echo "<br />".$sql;

        $mysqli->query($sql);
        if (mysqli_error($mysqli)) {
            echo "<br />数据库插入错误：" . mysqli_error($mysqli);
        } else {
            mysqli_close($mysqli);
//echo "<br />注册成功<br />";
            header("Location: user-signin.html?email=$email&signup_flag=1");
            exit;
        }
    }
} elseif ($flag == 1) {

    $sql = "select * from participant where `email`='$email';";
    $rst = $mysqli->query($sql);
    if ($repeat_password !== $password) {
        echo "<br />两次输入密码不一致<br />";
        echo '<a href="javascript :;" onClick="javascript :history.back(-1);">回到注册界面</a>';
    } else {
        $sql2 = "select * from participant where `email`='$email' and password='$password'";
        $rst2 = $mysqli->query($sql2);
        if ($rst2->num_rows == 0) {
            echo "<br />邮箱或密码错误，无法修改";
            echo '<a href="javascript :;" onClick="javascript :history.back(-1);">回到注册界面</a>';
        } else {
            $sql = "UPDATE participant SET name ='$name',sex='$sex',school='$school',job_title='$job_title',team_position='$team_position',school_position='$school_position',phone_number='$phone_number',bank_account='$bank_account',bank='$bank',clothing_model='$clothing_model',committee='$committee' WHERE `email` = '$email' and `password`='$password';";
            $mysqli->query($sql);
            echo "<br />修改成功";
            mysqli_close($mysqli);
        }
    }
}
?>